<!-- Horizontal Form -->
 <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">添加角色</h3>
      </div>
      <!-- /.box-header -->
      <!-- form start -->
      <form class="form-horizontal">
        <div class="box-body">
          <div class="form-group">
            <label for="nameId" class="col-sm-2 control-label">名称</label>

            <div class="col-sm-10">
              <input type="text" class="form-control" name="name" id="nameId" placeholder="名称">
            </div>
          </div>
          <div class="form-group">
            <label for="noteId" class="col-sm-2 control-label">描述</label>

            <div class="col-sm-10">
              <input type="text" class="form-control" name="note" id="noteId" placeholder="描述">
            </div>
          </div>
          <div class="form-group">
				<label class="col-sm-2 control-label">授权</label>
				<div class="col-sm-10">
						<ul id="menuTree" class="ztree"></ul>
				</div>
		  </div>
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
          <button type="button" class="btn btn-default btn-cancel">Cancel</button>
          <button type="button" class="btn btn-info pull-right btn-save">Save</button>
        </div>
        <!-- /.box-footer -->
      </form>
    </div>
    <script type="text/javascript" src="bower_components/ztree/jquery.ztree.all.min.js"></script>
    <script type="text/javascript">
    var ztree; 
    var setting = {
   	data : {
   	    simpleData : {
   			enable : true,
   			idKey : "id",  //节点数据中保存唯一标识的属性名称
   			pIdKey : "parentId",  //节点数据中保存其父节点唯一标识的属性名称
   			rootPId : null  //根节点id
   		}
   	},
   	check:{
   		enable:true,
   		nocheckInherit:true
   	}
    }
    $(function(){
    	doLoadSysMenus();
    	$(".box-footer")
    	.on("click",".btn-cancel",doCancel)
    	.on("click",".btn-save",doSaveOrUpdate)
    });
    function doCancel(){
       var url="role/doRoleListUI.do";
       $("#mainContentId").removeData();
       $("#mainContentId").load(url);
    }
    function doSaveOrUpdate(){
    	
       //1.params
       var params=doGetEditFormData();
       var rowData=$("#mainContentId").data("roleData")
       if(rowData)params.id=rowData.role.id
       var insertUrl="role/doUpateObject.do"
       
       //2.url
       var updateUrl="role/doSaveObject.do";
       var url=rowData?updateUrl:insertUrl;
       //3.ajax
       $.post(url,params,function(result){
    	   if(result.state==1){
    		   alert(result.message);
    		   doCancel();
    	   }else{
    		   alert(result.message);
    	   }
       })
    }
    function doGetEditFormData(){
    	//获取用户输入的角色名称,角色备注信息
    	var params={
    	   name:$("#nameId").val(),
    	   note:$("#noteId").val()
    	}
    	//获取用户选择的菜单对象id
    	var array=[];
    	var nodes=zTree.getCheckedNodes(true);
    	for(var i in nodes){
    		array.push(nodes[i].id)
    	}
    	params.menuIds=array.toString();
    	return params;
    }
    
    
    
    
    //初始化页面的菜单信息(资源信息)
    function doLoadSysMenus(){
    	
    	var url="menu/doFindZtreeMenuNodes.do";
    	$.getJSON(url,function(result){
    		if(result.state==1){
    			zTree=$.fn.zTree.init(
    				$("#menuTree"),
    				setting,
    				result.data);
    			
    			var rowData=$("#mainContentId").data("roleData");
    			
    			if(rowData)doInitEditForm(rowData);
    		}else{
    			alert(result.message);
    		}
    	});
    }
    
    function doInitEditForm(rowData){
    	
    	$("#nameId").val(rowData.role.name)
    	$("#noteId").val(rowData.role.note)
    	zTree.expandAll(true)
    	
    	var menuIds=rowData.menuIds
    	
    	for(var i in menuIds){
    		var node=zTree.getNodeByParam("id",menuIds[i]);
    		zTree.checkNode(node,true,false)
    	}
    }
    </script>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    